<style>
div.user_info{flex-direction: column;align-items: flex-start}
div.user_info img{height: 1.7em;}
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #user_info="{row}">
      <div class="user_info flex">
        <p class="flex"><img v-if="row.head_img" :src="row.head_img" alt="">{{row.nickname}}</p>
        <p>电话：{{row.phone}}</p>
      </div>
    </template>
    <template #action="{row}">
      <el-button @click="show_pay_return(row)" size="small">详情</el-button>
      <el-button @click="show_link(row)" size="small">关联数据</el-button>
      <el-button v-if="row.status==4" @click="show_verify(row)" size="small">审核</el-button>
      <el-button v-if="row.need_sharing==1" @click="show_sharing(row)" size="small">分账情况</el-button>
    </template>
  </list_url>

  <el-dialog v-model="dialog_verify.show" title="审核" width="600px" style="max-height: 90vh;margin: 5vh auto;" :draggable="true" @closed="dialog_verify.show=false">
    <forms :remove_name="1" :config="dialog_verify.config" :btns="dialog_verify.btns"></forms>
  </el-dialog>

  <el-dialog v-model="dialog_pay_return.show" title="支付记录" width="80%" style="max-height: 90vh;margin: 5vh auto;" :draggable="true" @closed="dialog_pay_return.show=false">
    <log_list :value="dialog_pay_return.log_json" desc="1" is_info="1"></log_list>
  </el-dialog>

  <el-dialog v-model="dialog_sharing.show" title="分账情况" width="600px" :draggable="true" @closed="dialog_sharing.show=false">
    <p>状态：{{ dialog_sharing.sharing_status_map[dialog_sharing.sharing_status]||'-' }}</p>
  </el-dialog>

</template>
<script>
import {_config, json_decode, json_decode_object, json_encode} from "@/js/s94_tool.js";
import {S94Model} from "@/js/S94Model.js";
import {iframe, in_right_admin} from "@/js/tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";

export default {
  components: {},

  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if (is_del){
      if(in_right_admin('LogPay/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      if(in_right_admin('LogPay/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {
      if(in_right_admin('LogPay/add')) btns['添加'] = function(){ iframe('log_pay/edit') }
      if(in_right_admin('LogPay/del')) btns['删除'] = function(){ app.$refs['list_url'].del() }
      if(in_right_admin('LogPay/recycle')) btns['回收站'] = function(){ iframe('log_pay/index?is_del=1') }
    }
    let status_map = '全部|,取消|0,待支付|1,已支付|2,支付失败|3';
    let pay_type_map = '全部|,余额|yue,微信|wechat,微信服务商|wechat_partner,线下支付|offline';
    let forms_config = {
      create_time: {label:'下单时间',name:'where[create_time][between]',config:'Y-M-D',type:'date_between',value:'',add_radio:true},
      status: {label:'状态',name:'where[status][eq]',config:status_map,type:'radio',button:1,value:''},
      pay_type: {label:'支付方式',name:'where[pay_type][like]',type:'select_one', config:pay_type_map,value:''},
      phone: {label:'用户电话',name:'where[user.phone][eq]',type:'text_one',value:''},
      pay_no: {label:'支付单号',name:'where[pay_no][like]',type:'text_one',value:''},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', order:true},
      pay_no: {name:'pay_no', label:'支付单号', width:'120px', type:'text_more'},
      sys_pay_no: {name:'sys_pay_no', label:'第三方单号', width:'120px', type:'text_more'},
      user_info: {name:'user_info', label:'支付用户', width:'140px', type:'user_info'},
      pay_type: {name:'pay_type', label:'支付方式', width:'80px', type:'select_one', config:pay_type_map},
      pay_money: {name:'pay_money', label:'支付金额', width:'80px', type:''},
      total_money: {name:'total_money', label:'总支付金额', width:'80px', type:''},
      refund_money: {name:'refund_money', label:'已退款', width:'80px', type:''},
      over_money: {name:'over_money', label:'已成交', width:'80px', type:''},
      status: {name:'status', label:'状态', width:'80px', type:'select_one', config:status_map},
      need_sharing: {name:'need_sharing', label:'是否分账', width:'60px', type:'select_one', config:'否|0,是|1'},
      pay_time: {name:'pay_time', label:'支付时间', width:'120px', type:''},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'下单时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'140px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/log_pay/'+(is_del?'recycle':'list'),
        title: "支付记录列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },

      dialog_verify: {
        show: false,
        config: {
          pay_no: {label:'支付单号',name:'pay_no',type:'text_one',value:'', is_info:true},
          pay_money: {label:'支付金额',name:'pay_money',type:'text_one',value:'', is_info:true},
          total_money: {label:'总支付金额',name:'total_money',type:'text_one',value:'', is_info:true},
          pay_verify_file: {label:'支付凭证',name:'pay_verify_file',type:'file_imgs',value:'', is_info:true},
          pay_verify_note: {label:'支付备注',name:'pay_verify_note',type:'text_one',value:'', is_info:true},
        },
        btns: [
          {title:'不通过', type:'danger', onclick(){app.edit_verify({status:3})}},
          {title:'通过', type:'primary', onclick(){app.edit_verify({status:2})}},
        ],
        list_row: {},
      },
      dialog_pay_return: {
        show: false,
        log_json: '[]',
      },
      dialog_sharing: {
        show: false,
        sharing_status_map: ['默认','待分账','已发起','分账成功','分账失败','无需分账','分账已退回'],
        data: [],
        sharing_status: 0,
      },
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('LogPay/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('log_pay/edit?'+s94Mjs.param_encode(param))
    },
    edit_verify(data){
      let id = this.dialog_verify.list_row.id;
      let url = `/admin/log_pay/edit_verify?id=`+id;
      ajax({url:url, method:'POST', data:data, loading:true}, (res)=>{
        this.$refs.list_url.list();
        this.dialog_verify.show = false;
      })
    },
    show_verify(row){
      this.dialog_verify.list_row = row;
      this.dialog_verify.config.pay_no.value = row.pay_no;
      this.dialog_verify.config.pay_money.value = row.pay_money;
      this.dialog_verify.config.total_money.value = row.total_money;
      let extend = {};
      try {
        if (row.extend) extend = JSON.parse(row.extend);
      } catch (e) {}
      extend = extend && typeof extend === 'object' ? extend : {};
      this.dialog_verify.config.pay_verify_file.value = extend.pay_verify_file;
      this.dialog_verify.config.pay_verify_note.value = extend.pay_verify_note;
      this.dialog_verify.show = true;
    },
    show_pay_return(row){
      let log_list = Object.values(json_decode_object(row.pay_return));
      log_list.forEach(row=>{
        row.content = row.data;
      })
      this.dialog_pay_return.log_json = json_encode(log_list);
      this.dialog_pay_return.show = true;
    },
    show_link(row){
      if (!row.link_table || !row.link_id) return this.$message('无关联数据');
      let model = S94Model.find(row.link_table);
      let info_action = model ? model.first_action('info', true) : null;
      if (!info_action) return this.$message('关联数据缺少详情操作，无法展示');
      iframe(`/model_v2_base/edit?model_name=${row.link_table}&action=${info_action.action}&is_info=1&id=${row.link_id}`);
    },
    show_sharing(row){
      this.dialog_sharing.sharing_status = row.sharing_status;
      this.dialog_sharing.data = row.sharing_receiver;
      this.dialog_sharing.show = true;
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

  }
}
</script>
